package com.moto.mapper;

import com.moto.entity.Address;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moto.entityDto.AddressUpdateDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author hyf
 * @since 2023-09-04
 */
@Mapper
public interface AddressMapper extends BaseMapper<Address> {

    /**
     * 新增地址
     * @param address
     */
    @Insert("INSERT INTO address (province,city,county,address_detail,area_code,is_default,name,tel,u_id,is_delete) VALUES (#{province},#{city},#{county},#{addressDetail},#{areaCode},#{isDefault},#{name},#{tel},#{uId},1)" +
            "")
    void addAddress(Address address);


    /**
     * 修改地址
     * @param addressUpdateDto
     */
    @Transactional
    @Update("UPDATE address SET province=#{province},city=#{city},county=#{county},address_detail=#{addressDetail},area_code=#{areaCode},is_default=#{isDefault},name=#{name},tel=#{tel},is_delete=1 WHERE address_id=#{id};")
    void updateAddressById(AddressUpdateDto addressUpdateDto);

    /**
     * 修改默认地址
     * @param id
     */
    @Transactional
    @Update("update address set is_default = 0 where u_id=#{id}")
    void updateIsDefault(int id);
}
